﻿using DataAccess.DTO;
using DataAccess.Models;
using Solr;
using Solr.Commons;
using Solr.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.Solr
{
  
   public class SolrIndexDetailBLL
   {
       static ISolrResponseParser<NamedList, ResponseHeader> binaryResponseHeaderParser = new BinaryResponseHeaderParser();
       static IObjectDeserializer<ProductDetail> exampleDeserializer = new ProductDeserializer();
       static ISolrResponseParser<NamedList, QueryResults<ProductDetail>> binaryQueryResultsParser = new BinaryQueryResultsParser<ProductDetail>(exampleDeserializer);

       static ISolrResponseParser<NamedList, IList<GroupQueryResults<ProductDetail>>> GroupByeParser = new BinaryGroupQueryResultsParser<ProductDetail>(exampleDeserializer);

       static ISolrQueryConnection<NamedList> Productconnection = null;
       static ISolrQueryOperations<NamedList> Productoperations = null;
       static ISolrResponseParser<NamedList, IDictionary<string, IDictionary<string, IList<string>>>> binaryHighlightingParser = new BinaryHighlightingParser();
       private static string SolrPath = System.Configuration.ConfigurationManager.AppSettings["Solr"];
       static string ProductCollectionName = "product";
       public SolrIndexDetailBLL()
       {

       }
       static SolrIndexDetailBLL()
       {
           if (Productconnection == null && Productoperations == null)
           {

               Productconnection = new SolrQueryConnection<NamedList>() { ServerUrl = SolrPath };
               Productoperations = new SolrQueryOperations<NamedList>(Productconnection) { ResponseWriter = "javabin" };
           }

       }
       /// <summary>
       /// SolrQuestion反序列化器
       /// </summary>
       public class ProductDeserializer : IObjectDeserializer<ProductDetail>
       {
           public IEnumerable<ProductDetail> Deserialize(SolrDocumentList result)
           {
               foreach (SolrDocument doc in result)
               {
                   yield return new ProductDetail()
                   {

                       auctionId = doc["id"] != null ? long.Parse(doc["id"].ToString()) : 0,
                       auctionUrl = doc["auctionUrl"] != null ? doc["auctionUrl"].ToString() : "",
                       biz30day = doc["biz30day"] != null ? int.Parse(doc["biz30day"].ToString()) : 0,
                       couponActivityId = doc["couponActivityId"] != null ? doc["couponActivityId"].ToString() : "",
                       couponAmount = doc["couponAmount"] != null ? float.Parse(doc["couponAmount"].ToString()) : 0,
                       couponTotalCount = doc["couponTotalCount"] != null ? float.Parse(doc["couponTotalCount"].ToString()) : 0,
                       pictUrl = doc["pictUrl"] != null ? doc["pictUrl"].ToString().Contains("http") ? doc["pictUrl"].ToString(): "https:"+doc["pictUrl"].ToString(): "",
                        //原价格
                       zkPrice = doc["zkPrice"] != null ? float.Parse(doc["zkPrice"].ToString()) : 0,

                       //描述
                       Introduce = doc["Introduce"] != null ? doc["Introduce"].ToString() : "",
                       //类型
                       userType = doc["userType"] != null ? int.Parse(doc["userType"].ToString()) : 0,
                       shopTitle =!String.IsNullOrEmpty(doc["shopTitle"].ToString()) ? doc["shopTitle"].ToString() : "淘宝",
                       prices = doc["prices"] != null ? float.Parse(doc["prices"].ToString()) : 0,
                       title = doc["productname"] != null ? doc["productname"].ToString() : "",
                       DataTypeID= doc["DataTypeID"] != null ? int.Parse(doc["DataTypeID"].ToString()) : 0


                   };
               }
           }
       }
       /// <summary>
       /// 通过商品ID查询
       /// </summary>
       /// <param name="ProductSpecRelRid"></param>
       /// <returns></returns>
       public ProductDetail GetProductDetailByProductInfo(string auctionId)
       {
           ISolrQuery query = new SolrQuery("id:" + auctionId + "");

           var options = new Dictionary<string, ICollection<string>>();

           options.Add("start", new string[] { "0" });
           options.Add("rows", new string[] { "1" });

           var result = Productoperations.Query(ProductCollectionName, "/select", query, options);

           var examples = binaryQueryResultsParser.Parse(result);

           return examples.FirstOrDefault();
       }

     
   }
}
